package com.service.lotterySystem.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.lotterySystem.dto.AddLogisticsInfoDto;
import com.service.lotterySystem.dto.LogisticsInfoDto;
import com.service.lotterySystem.dto.UserDto;
import com.service.lotterySystem.service.LogisticsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;

/**
 * 物流编号表(支持上传excel)logistics_info）(LogisticsInfo)表控制层
 *
 * @author makejava
 * @since 2022-01-19 09:04:30
 */
@RestController
@RequestMapping("logisticsInfo")
@Api(value = "物流接口", tags = "物流接口")
public class LogisticsInfoController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsInfoService logisticsInfoService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 写入物流编号
     * @param addLogisticsInfoDto
     * @return 单条数据
     */
    @PostMapping(value = "addLogisticsInfo",produces="application/json")
    @ApiOperation(value = "写入物流编号")
    @ResponseBody
    public Result addLogisticsInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                   @RequestBody AddLogisticsInfoDto addLogisticsInfoDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return this.logisticsInfoService.addLogisticsInfo(addLogisticsInfoDto,userId);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }

    /**
     * 修改物流编号
     * @param
     * @return 单条数据
     */
    @PostMapping(value = "updateLogisticsInfo",produces="application/json")
    @ApiOperation(value = "修改物流编号")
    @ResponseBody
    public Result updateLogisticsInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                   @RequestBody LogisticsInfoDto logisticsInfoDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return this.logisticsInfoService.updateLogisticsInfo(logisticsInfoDto,userId);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }

    /**
     * excel上传物流编号
     * @param
     * @return
     */
    @PostMapping(value = "addLogisticsInfoByExcel",produces="application/json")
    @ApiOperation(value = "excel上传物流编号")
    @ResponseBody
    public Result addAddressByExcel(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                    @RequestParam("file")  @ApiParam("文件") MultipartFile file,
                                    @RequestParam("aid")@ApiParam("活动id") String aid) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return logisticsInfoService.addLogisticsInfoByExcel(file,aid,userDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }

    /**
     * 用户查询物流编号
     * @return 单条数据
     */
    @GetMapping(value = "queryLogisticsInfo",produces="application/json")
    @ApiOperation(value = "用户查询物流编号")
    @ResponseBody
    public Result queryLogisticsInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                     @RequestParam(value = "card") @ApiParam("card:0--用户侧 1--运营侧")Integer card,
                                     @RequestParam(value = "aId" ,required = false) @ApiParam("活动id")String aId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return logisticsInfoService.queryLogisticsInfo(userDto,card,aId);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.ok();
    }
}
